package com.zb.controller;

import com.zb.bean.Book;
import com.zb.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询全部的书籍，并且返回到一个书籍展示页面
    @RequestMapping("/allBook")
    public String list(Model model) {
        System.out.println(bookService);
        List<Book> lists = bookService.queryAllBook();
        model.addAttribute("list",lists);
        return "allBook";
    }

    //跳转到增加图书页面
    @RequestMapping("/toAddBook")
    public String toAddPaper() {
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String addPaper(Book book) {
        System.out.println(book);
        bookService.addBook(book);
        return "redirect:/book/allBook";//重定向
    }

    //跳转到修改页面
    @RequestMapping("/toUpdateBook")
    public String toUpdatePager(Model model,String id) {
        Book book = bookService.queryBookById(Integer.parseInt(id));
        System.out.println("查询到的书籍信息"+book);
        model.addAttribute("book",book);
        return "updateBook";
    }

    //修改书籍
    @RequestMapping("/updateBook")
    public String updatePage(Book book){
        bookService.updateBook(book);
        return "redirect:/book/allBook";//重定向
    }

    //删除数据
    @RequestMapping("/deleteBook/{bookID}")
    public String deletePager(@PathVariable("bookID") String id){
        bookService.deleteBookById(Integer.parseInt(id));
        return "redirect:/book/allBook";//重定向
    }

    //查询书籍
    @RequestMapping("queryBook")
    public String queryBook(String queryBookName,Model model) {
        List<Book> lists = bookService.queryBookByName("%" + queryBookName + "%");
        model.addAttribute("list",lists);
        return "allBook";
    }

}
